package com.swpu.project.util;

import com.swpu.project.model.entity.LoginUser;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.List;
import java.util.function.Function;

import static com.swpu.project.util.NumberUtil.findNumber;

public class AuthorityUtil
{


 public static boolean checkAuthority(List<String> suppliedBlockNum)
 {
        LoginUser principal = (LoginUser) SecurityContextHolder
                .getContext().getAuthentication().getPrincipal();
       if(principal.getJudge())
           return true;
        List<String>  accessibleBlockNums = principal.getBlockNums();
     for (int i = 0; i <suppliedBlockNum.size() ; i++) {

         String s = suppliedBlockNum.get(i);
         s=findNumber(s);
         if(!accessibleBlockNums.contains(s))
            return false;
     }



        return true;
 }


 public   static Boolean   checkAuthority(String blockNum)
 {
     LoginUser principal = (LoginUser) SecurityContextHolder
         .getContext().getAuthentication().getPrincipal();
     if (!principal.getBlockNums().contains(blockNum))
         return false;
     return true;
 }


public static <T> Boolean  checkAuthority(T target, Function<T,String> getBlocknum)
{

    LoginUser principal = (LoginUser)SecurityContextHolder.getContext().getAuthentication().getPrincipal();

    List<String> blockNums = principal.getBlockNums();
    String blockName = getBlocknum.apply(target);


    String number = findNumber(blockName);


    if(!blockNums.contains(number))
        return false;

    return true;





}











}
